<?php
/**
 * 权限
 */
namespace App\Models\MainDB\Access;

use Illuminate\Database\Eloquent\Model;

class Access extends Model
{
    protected $table = 'ln_access';

    protected $primaryKey = 'id';

    protected $fillable = [
        'id', //主键
        'name', //权限名称
        'menu_id', //所属菜单id
        'sort', //排序号，升序
        'admin_id', //最后操作的管理员id
    ];

    public $timestamps = false;

    public function add($data)
    {
        return $this->insertGetId($data);
    }

    public function edit($where, $data)
    {
        return $this->where($where)->update($data);
    }

    //查询所有权限，并根据menu_id映射
    public function getAccessWithMenuIdMap()
    {
        $data = $this->select('id', 'name', 'menu_id')->orderBy('sort')->get()->toArray();
        $access = [];
        foreach ($data as $v) {
            $access[$v['menu_id']][] = $v;
        }
        return $access;
    }
}